package cn.dt.ymcc.web.controller;

import cn.dt.ymcc.GlobalError;
import cn.dt.ymcc.GlobalException;
import cn.dt.ymcc.dto.RegisterDto;
import cn.dt.ymcc.result.JSONResult;
import cn.dt.ymcc.service.IVerifycodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifycode")
public class VerifycodeController {
    @Autowired
    private IVerifycodeService verifycodeService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @GetMapping("/imageCode/{imageCodeKey}")
    public JSONResult image(@PathVariable("imageCodeKey") String key){
        try {
            String base64 = verifycodeService.imageCode(key);
            return JSONResult.success(base64);
        } catch (GlobalException e) {
            e.printStackTrace();
            return JSONResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.error(GlobalError.ERROR_EXCEPTION.getMessage());
        }
    }
    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @PostMapping("/sendSmsCode")
    public JSONResult sendSmsCode(@RequestBody RegisterDto dto){
        try {
            verifycodeService.sendSmsCode(dto);
            return JSONResult.success();
        } catch (GlobalException e) {
            e.printStackTrace();
            return JSONResult.error(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(dto.toString());
            return JSONResult.error(GlobalError.ERROR_EXCEPTION.getMessage());
        }
    }


}

